package com.huilian.iotbox.data.service.impl;

import com.huilian.iotbox.data.enums.MessageLogResultEnum;
import com.huilian.iotbox.data.enums.MessageLogTypeEnum;
import com.huilian.iotbox.data.po.MessageLog;
import com.huilian.iotbox.data.producer.MessageLogRabbit;
import com.huilian.iotbox.data.service.BlueToothPubService;
import com.huilian.iotbox.data.service.DeviceCommonService;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 谢洛涛
 * @date 2024/5/30 14:48
 */
@Service
@Slf4j
public class BlueToothPubServiceImpl implements BlueToothPubService {
    @Autowired
    private ErrorLogCommonService errorLogCommonService;
    @Autowired
    private MessageLogRabbit messageLogRabbit;

    @Override
    public boolean pub(String command, String deviceNo, String username) {
        MessageLog messageLog = new MessageLog();
        messageLog.setContent(command);
        messageLog.setDeviceNo(deviceNo);
        messageLog.setType(MessageLogTypeEnum.SEND.getCode());
        messageLog.setCreateTime(new Date());
        messageLog.setUsername(username);
        try {
            // 发送mq给蓝牙模块
            messageLogRabbit.sendBlueToothMessageToConsume(messageLog);
            return true;
        } catch (Exception e) {
            log.error("消息发送失败，发送出现异常");
            errorLogCommonService.writerInfoErrorLog("消息发送失败，发送出现异常", e);
            messageLog.setResult(MessageLogResultEnum.FAIL.getCode());
            e.printStackTrace();
            return false;
        } finally {
            messageLogRabbit.sendMessageLog(messageLog);
        }
    }
}
